package com.ciei.dpagm.service;

import com.ciei.dpagm.common.service.BaseService;
import com.ciei.dpagm.entity.FormPropertyOption;
import com.ciei.dpagm.mapper.FormPropertyOptionMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wangyan
 * @date 2021/07/02
 */
@Service
public class FormPropertyOptionService extends BaseService<FormPropertyOptionMapper, FormPropertyOption> {

    /**
     * 根据数据范围插入选择框选项
     * @param propertyDefinition
     * @param validValues
     * @param displayValues
     */
    public void insertFromValues(Integer propertyDefinition,String validValues,String displayValues){
        List<FormPropertyOption> propertyOptionList = new ArrayList<>();
        String[] validValueArray = validValues.split(",");
        for (int i = 0; i < validValueArray.length; i++) {
            FormPropertyOption propertyOption = new FormPropertyOption();
            propertyOption.setPropertyDefinitionId(propertyDefinition);
            propertyOption.setValue(validValueArray[i]);
            if (StringUtils.isBlank(displayValues)){
                propertyOption.setLabel(validValueArray[i]);
            }else {
                propertyOption.setLabel(displayValues.split(",")[i]);
            }
            propertyOptionList.add(propertyOption);
        }
        if (!propertyOptionList.isEmpty()){
            saveBatch(propertyOptionList);
        }
    }

}
